/**
 * 
 */
package com.gosophia.govDoc.action.transientDoc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.govDoc.dto.TransientDocumentDTO;
import com.gosophia.govDoc.ws.TransientDocumentWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 功能：
 * 
 * @author zhangdongsheng
 * @since 2011-2-9 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@SuppressWarnings("serial")
@Controller("TransientDocAction_1")
@Scope("prototype")
public class TransientDocAction_1 extends ActionSupport {

    private Long jobId;
    private TransientDocumentDTO transientDocumentDto;
    @Autowired
    private TransientDocumentWebService transientDocumentWebService;
    @Autowired
    private TaskManagementWebService taskManagementWebService;
    private String fromFlag;

    @Override
    public String execute() throws Exception {

        // 得到当前用户
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
                .getUserInfoId();
        if (jobId != null) {
            fromFlag = "task";
            convertMissionInfoToTransient(creatorId, jobId);// 保存任务信息到
        } else {
            transientDocumentDto.setCreator(creatorId);
            fromFlag = "bussiness";
            transientDocumentWebService
                    .saveOrUpdateTransientDocument(transientDocumentDto);
        }

        transientDocumentDto = transientDocumentWebService
                .findTranDocByCreatorAndJobId(creatorId, jobId);

        return SUCCESS;
    }

    private void convertMissionInfoToTransient(long creatorId, Long jobId)
            throws BusinessException {
        transientDocumentDto = transientDocumentWebService
                .findTranDocByCreatorAndJobId(creatorId, jobId);
        if (transientDocumentDto == null) {
            MissionDTO md;
            JobDTO jobDTO = taskManagementWebService
                    .findJobDetailByJobId(jobId);
            md = jobDTO.getMissionDTO();
            TransientDocumentDTO dto = new TransientDocumentDTO();
            dto.setMissionName(md.getMissionName());
            dto.setCreator(md.getCreatorDTO().getUserInfoId());
            dto.setMissionEnd(md.getScheduleEndDate());
            dto.setJobId(jobId);// 关键是设定jobId
            transientDocumentWebService.saveOrUpdateTransientDocument(dto);
        }
    }

    public TransientDocumentDTO getTransientDocumentDto() {
        return transientDocumentDto;
    }

    public void setTransientDocumentDto(
            TransientDocumentDTO transientDocumentDto) {
        this.transientDocumentDto = transientDocumentDto;
    }

    public Long getJobId() {
        return jobId;
    }

    public void setJobId(Long jobId) {
        this.jobId = jobId;
    }

    public String getFromFlag() {
        return fromFlag;
    }

    public void setFromFlag(String fromFlag) {
        this.fromFlag = fromFlag;
    }

}
